package com.wlstock.funi.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.wlstock.funi.model.SelfFundTagModel;

import java.sql.SQLException;
import java.util.List;



public class SelfFundTagDao{
	private DataBaseHelper dbHelper;
	private static SelfFundTagDao daoInstance;
	private Dao<SelfFundTagModel, Integer> dao;
	
	@SuppressWarnings("unchecked")
	private SelfFundTagDao(Context mContext){
		try {
			dbHelper = DataBaseHelper.getHelper(mContext);
			dao = dbHelper.getDao(SelfFundTagModel.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static SelfFundTagDao getInstance(Context mContext){
		if (daoInstance == null) {
			synchronized (SelfFundTagDao.class) {
				if (daoInstance == null) {
					daoInstance = new SelfFundTagDao(mContext);
				}
			}
		}
		return daoInstance;
	}
	
	
	public void createOrUpdate(SelfFundTagModel model){
		try {
			dao.createOrUpdate(model);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<SelfFundTagModel> queryAllTags() {
		try {
		 	return dao.queryForAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String[]  queryAllTagNames(){
		List<SelfFundTagModel> tags = queryAllTags();
		if (tags!=null) {
			String[] tagNames = new String[tags.size()];
			for (int i = 0; i < tags.size(); i++) {
				String tag = tags.get(i).getTag();
				tagNames[i] = tag;

			}
			return tagNames;
		}
		return null;
	}

	/**
	 * 删除某一个Tag标签
	 * @param tagModel
     */
	public void deleteWith(SelfFundTagModel tagModel) {
		try {
			dao.delete(tagModel);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deleteWithTagName(String tagName){
        try {
            int i = dao.updateRaw("delete from SelfFundTagModel where tag = ?", tagName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
